import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/main/Home.vue'
import Layout from '../views/Layout.vue'
// import Login from "@/views/Login";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'Layout',
        component: Layout,
        children: [
            {
                path: '/',
                name: Home,
                component: Home
            },
            {
                path: "fantasy",
                name: "Fantasy",
                component: () => import("../views/main/Fantasy.vue")
            },
            {
                path: "wuxia",
                name: "Wuxia",
                component: () => import("../views/main/Wuxia.vue")
            },
            {
                path: "history",
                name: "History",
                component: () => import("../views/main/History.vue")
            },
            {
                path:"details",
                name:"Details",
                component: ()=>import("../views/main/Details.vue")
            },
            {
                path:"search",
                name:"Search",
                component: ()=>import("../views/main/Search.vue")
            },
            {
                path:"InfoUser",
                name:"InfoUser",
                component: ()=>import("../views/main/InfoUser.vue")
            }
        ]
    },
    {
        path: "/login",
        name: "Login",
        component: () => import("../views/Login.vue")
    },
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const token = localStorage.getItem('Authorization')
    if (!token) {
        if (to.name !== 'Login') {
            next({name: 'Login'})
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router
